package com.lu.model.enums;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;

/**
 * @Description: 用户交易记录来源类型
 * @Author: zhanglu
 * @Date: 2022/8/12 11:03
 */
@Getter
@AllArgsConstructor
public enum UserTransactionSourceTypeEnum {

  USER_BIND_CARD(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.BIND_CARD, "用户绑卡"),
  USER_TRANSFER_ADD(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.TRANSFER_ADD, "用户转增-增加"),
  USER_TRANSFER_REDUCE(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.TRANSFER_REDUCE, "用户转增-减少"),
  USER_WITHDRAWAL(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.WITHDRAWAL, "用户提现"),
  USER_OFFLINE_STORE_CONSUME(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.CONSUME, "用户线下消费(门店核销)"),
  USER_ORDER_REFUND(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.REFUND, "订单退款"),
  USER_OFFLINE_STORE_CONSUME_REFUND(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.REFUND, "用户线下消费退款(门店核销)"),

  ORDER_CONSUME(UserTransactionSourceEnum.ORDER, UserTransactionTypeEnum.CONSUME, "订单消费"),
  ORDER_REFUND(UserTransactionSourceEnum.ORDER, UserTransactionTypeEnum.REFUND, "订单退款"),
  ORDER_WITHDRAWAL(UserTransactionSourceEnum.ORDER, UserTransactionTypeEnum.WITHDRAWAL, "订单提现"),

  SYSTEM_AUTO_EXPIRE(UserTransactionSourceEnum.USER, UserTransactionTypeEnum.AUTO_EXPIRE, "系统自动过期"),

  ;

  private UserTransactionSourceEnum source;
  private UserTransactionTypeEnum type;
  private String description;

  public BigDecimal negateAmount(BigDecimal amount){
    if(ObjectUtil.isEmpty(amount)){
      return null;
    }
    if(this.getType().getInOrOut().equals("-") && amount.compareTo(BigDecimal.ZERO) > 0){
      return amount.negate(); //只返回负数
    }
    return amount;
  }

  @Getter
  @AllArgsConstructor
  public enum UserTransactionTypeEnum {

    //类型（绑卡、转增、消费、退款、提现、过期）
    BIND_CARD("绑卡", "+"),
    TRANSFER_ADD("转增-增加", "+"),
    TRANSFER_REDUCE("转增-减少", "-"),
    CONSUME("消费", "-"),
    REFUND("退款", "+"),
    WITHDRAWAL("提现", "-"),
    AUTO_EXPIRE("自动过期", "-"),

    ;
    private String description;
    /**
     * - : 支出-账户减少
     * + : 收入-账户增加
     * @see YesOrNoEnum
     */
    private String inOrOut;

  }

  @Getter
  @AllArgsConstructor
  public enum UserTransactionSourceEnum {

    ORDER("订单", "order_aggregation_item"),
    CARD("卡片", "card"),
    USER("用户", "user_integral"),

    ;

    private String description;
    private String tableName;

    public static UserTransactionSourceEnum getByName(String source){
      if(ObjectUtil.isEmpty(source)){
        return null;
      }
      for (UserTransactionSourceEnum value : UserTransactionSourceEnum.values()) {
        if(source.equalsIgnoreCase(value.name())){
          return value;
        }
      }
      return null;
    }
  }


}


